﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ExpenseSystem.Common;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.Lor;

namespace ExpenseSystem.Core.Converter
{
    public static class ProfileConverter
    {
        public static UserProfile FromUser(UserModel user,long site)
        {
            UserProfile userProfile = new UserProfile();
            userProfile.Admin = user.IsAdmin;
            userProfile.LastLogin = user.LastLogin;
            userProfile.UserCode = user.Code;
            userProfile.UserId = user.AutoId;
            userProfile.UserName = user.Name;
            userProfile.SpCtl = user.IsSpCtl;
            userProfile.IpAddress = HttpContext.Current.Request.UserHostAddress;
            string message;
            LogHelper.Info(typeof(ProfileConverter).Name,String.Format("正在为用户[{0}]生成Profile",user.Code));
            if (site != 0)
            {
                SiteFetcher siteFetcher = new SiteFetcher();
                SiteModel siteModel = siteFetcher.Get( site, out message);
                if (siteModel == null)
                {
                    LogHelper.Error(typeof(ProfileConverter).Name, String.Format("{0}正在尝试登陆一个不存在的据点{1},{2}", user.Code,site,message));
                    return userProfile;
                }
                else
                {
                    userProfile.SiteId = site;
                    userProfile.SiteName = siteModel.Name;
                }
            }
            else
            {
                LogHelper.Error(typeof(ProfileConverter).Name, String.Format("{0}正在尝试登陆一个不存在的据点{1}", user.Code, site));
                return userProfile;
            }
            RoleSiteFetcher roleSiteFetcher = new RoleSiteFetcher();
            RoleUserFetcher roleUserFetcher = new RoleUserFetcher();
            int rows;
            IList<RoleSiteModel> roleSiteModel = roleSiteFetcher.ListBySiteId(site,out message,out rows);
            IList<RoleUserModel> roleUserModel = roleUserFetcher.ListByUserId(user.AutoId,out message,out rows);
            var roleSiteIdList = from record in roleSiteModel select record.RoleId;
            var roleUserIdList = from record in roleUserModel select record.RoleId;
            userProfile.RoleList = roleSiteIdList.Intersect(roleUserIdList).ToArray();
            RoleFetcher roleFetcher = new RoleFetcher();
            Restrain restrain = Restrain.Eq("AutoId",-1);
            if (userProfile.RoleList.Length>0)
                 restrain =  Restrain.In("AutoId", userProfile.RoleList);

            IList<RoleModel> roleModels = roleFetcher.List( OrderBy.GetDefault(), Limit.GetDefault(), out message, out rows,restrain);
            foreach(RoleModel role in roleModels)
            {
                userProfile.RoleName = role.Name + ";";
            }
            LogHelper.Info(typeof(ProfileConverter).Name, String.Format("成功获得当前用户[{0}]在据点{1}的权限：{2}", user.Code, userProfile.SiteName, userProfile.RoleName));
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            EmployeeModel employee = employeeFetcher.GetEmployeeByUser( site, user.AutoId, out message);
            if(employee!=null)
            {
                userProfile.TitleId = employee.TitleId;
                userProfile.EmployeeCode = employee.Code;
                userProfile.EmployeeName = employee.Name;
                userProfile.EmployeeId = employee.AutoId;
                LogHelper.Info(typeof(ProfileConverter).Name, String.Format("当前登录用户{0}匹配的员工为{1}", user.Code,userProfile.EmployeeName));
            }
            else
            {
                LogHelper.Error(typeof(ProfileConverter).Name, String.Format("当前登录用户{0}未有找到匹配的员工", user.Code));
                return userProfile;
            }
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            DepartmentModel department = departmentFetcher.Get( site, employee.DepartmentId, out message);
            if (department != null)
            {
                userProfile.Department = department.Name;
                userProfile.DepartmentId = department.AutoId;
                LogHelper.Info(typeof(ProfileConverter).Name, String.Format("当前登录用户{0}匹配的部门为{1}", user.Code, userProfile.Department));

            }
            SalesPointEmployeeFetcher salesPointEmployeeFetcher = new SalesPointEmployeeFetcher();
            SalesPointEmployeeModel salesPointEmployeeModel = salesPointEmployeeFetcher.GetByEmployee(site,userProfile.EmployeeId,out message);
            if(salesPointEmployeeModel!=null)
            {
                userProfile.SalesPoint = salesPointEmployeeModel.SalesPointId;

            }
            SalesPointFetcher salesPointFetcher = new SalesPointFetcher();

            SalesPointModel salesPoint = salesPointFetcher.Get(site,userProfile.SalesPoint,out message);
            if (salesPoint != null)
                userProfile.Region = salesPoint.RegionId;
            return userProfile;
        }
    }
}
